package com.itboye.ihomebank.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.mobileim.lib.model.message.IFileMessage;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.activity.login.AddDeviceActivity;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.activity.me.ActivityChaKanShiFu;
import com.itboye.ihomebank.activity.guanjia.ActivityPingJiaWorker;
import com.itboye.ihomebank.activity.me.ActivityXiuGaiMoney;
import com.itboye.ihomebank.activity.guanjia.ActivityXiuLiZhiFu;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.dialog.RxDialog;
import com.itboye.ihomebank.widget.VerificationCodeView;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 我的生活管家（派单中,维修中，待评价）
 */
public class MyGuanJiaAdapter extends MutiplyCommonAdapter<ChanKanWeiXiuBean> implements Observer {
    List<ChanKanWeiXiuBean> datas;
    int position;
    int types;//类型
    Context context;

    protected String id;
    protected String money;
    RepairPersenter repairPersenter;
    protected String uid;
    protected String repairStatus;

    String reMoney;
    public MyGuanJiaAdapter(int types, Context context, List<ChanKanWeiXiuBean> datas,
                            int... layoutId) {
        super(context, datas, layoutId);
        this.datas = datas;
        this.context = context;
        this.types = types;
        repairPersenter = new RepairPersenter(this);
        uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
    }

    @Override
    public int getItemViewType(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void getItemPosition(int position) {
        this.position = position;
    }

    @Override
    public void convert(ViewHolder holder, final ChanKanWeiXiuBean t, int type,
                        int position) {
        holder.setText(R.id.guanjia_danhao, t.getId());
        holder.setText(R.id.guanjia_leixing, t.getRepairstatus());
        holder.setText(R.id.guanjia_address, t.getAddress());
        holder.setText(R.id.guanjia_name, t.getUname());

        if (datas.get(position).getStatus().equals("0") ) {
            holder.setText(R.id.guanjia_01, "师傅已到");
            holder.setVisible(R.id.guanjia_01,false);
            holder.setText(R.id.guanjia_02, "取消申请");
            holder.setVisible(R.id.guanjia_03, false);
        } else if (datas.get(position).getStatus().equals("1")){
            holder.setText(R.id.guanjia_01, "师傅已到");
            holder.setVisible(R.id.guanjia_01,true);
            holder.setText(R.id.guanjia_02, "查看师傅");
            holder.setVisible(R.id.guanjia_03, false);
        }else if (datas.get(position).getStatus().equals("2")) {
            holder.setText(R.id.guanjia_01, "查看师傅");
             holder.setVisible(R.id.guanjia_01,false);
            holder.setText(R.id.guanjia_02, "修理完成");
            holder.setVisible(R.id.guanjia_02,false);
            holder.setText(R.id.guanjia_03, "维修价格");
        } else if (datas.get(position).getStatus().equals("3")) {
            if (t.getPayStatus().equals("0")) {
                holder.setText(R.id.guanjia_01, "去支付");
            } else {
                holder.setText(R.id.guanjia_01, "评价师傅");
            }
            holder.setVisible(R.id.guanjia_02, false);
            holder.setVisible(R.id.guanjia_03, false);
        } else if (types == 4) {
            if (t.getStatus().equals("4")) {
                holder.setText(R.id.guanjia_01, "已完结");
            } else {
                holder.setText(R.id.guanjia_01, "评价师傅");
            }
            holder.setVisible(R.id.guanjia_02, false);
            holder.setVisible(R.id.guanjia_03, false);
        }
        holder.setOnClickListener(R.id.guanjia_01, position, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text = (TextView) v;
                if (text.getText().toString().equals("师傅已到")) {
                    id = t.getId();
                    repairPersenter.shiFuYiDao(t.getId());
                } else
                if (text.getText().toString().equals("查看师傅")) {
                    Intent intent = new Intent(context,
                            ActivityChaKanShiFu.class);
                    intent.putExtra("head", t.getHead());
                    intent.putExtra("name", t.getWname());
                    intent.putExtra("phone", t.getWmobile());
                    intent.putExtra("id", t.getWid());
                    context.startActivity(intent);
                } else if (text.getText().toString().equals("去支付")) {
                    if (SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("0")
                            ||SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("2")){//租客
                        id = t.getId();
                        money = t.getMoney();
                        repairStatus = t.getRepairstatus();
                        Intent intent = new Intent(context,ActivityXiuLiZhiFu.class);
                        intent.putExtra("id", id);
                        intent.putExtra("money", money);
                        intent.putExtra("type", repairStatus);
                        context.startActivity(intent);
                    }else if (SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("1")){//经纪人
                        ByAlert.alert("经纪人需到联盟店后台支付");
                    }



                } else if (text.getText().toString().equals("评价师傅"))  {
                    Intent intent = new Intent(context,
                            ActivityPingJiaWorker.class);
                    intent.putExtra("head", t.getHead());
                    intent.putExtra("name", t.getWname());
                    intent.putExtra("phone", t.getWmobile());
                    intent.putExtra("wid", t.getWid());
                    intent.putExtra("id", t.getId());
                    context.startActivity(intent);
                }
            }
        });

        holder.setOnClickListener(R.id.guanjia_02, position, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text = (TextView) v;
                if (text.getText().toString().equals("取消申请")) {
                    id=t.getId();
                    repairPersenter.shenQingWeiXiu("", "", "", "", "", "",
                            "", "", "", "2", t.getId());
                } else if (text.getText().toString().equals("修理完成")) {
                    id = t.getId();
                    money = t.getMoney();
                    repairStatus = t.getRepairstatus();
                    repairPersenter.xiuLiEnd(t.getId(), uid);
                } else if (text.getText().toString().equals("查看师傅")) {
                    Intent intent = new Intent(context,
                            ActivityChaKanShiFu.class);
                    intent.putExtra("head", t.getHead());
                    intent.putExtra("name", t.getWname());
                    intent.putExtra("phone", t.getWmobile());
                    intent.putExtra("id", t.getWid());
                    context.startActivity(intent);
                }
            }
        });

        holder.setOnClickListener(R.id.guanjia_03, position, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView text = (TextView) v;
                if (text.getText().toString().equals("维修价格")) {
                    Intent intent = new Intent(context,
                            ActivityXiuGaiMoney.class);
//                    intent.putExtra("id", t.getId());
//                    intent.putExtra("mobile", t.getWmobile());
//                    context.startActivity(intent);
                    EditextDialog dialog=new EditextDialog(context,t);
                    dialog.show();
                }
            }
        });

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (!entity.getCode().equals(0)) {
                //取消维修
                if (entity.getEventType() == RepairPersenter.shenQingWeiXiu_success) {
                    ByAlert.alert(entity.getData());
                    Iterator<ChanKanWeiXiuBean> iterator = datas.iterator();
                    while (iterator.hasNext()) {
                        ChanKanWeiXiuBean bean = iterator.next();
                        if (id.equals(bean.getId())) {
                            iterator.remove();
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
                if (entity.getEventType() == RepairPersenter.shenQingWeiXiu_fail) {
                    ByAlert.alert(entity.getData());
                }
                //师傅已到
                if (entity.getEventType() == RepairPersenter.yiDao_success) {
                    ByAlert.alert(entity.getData());
                    Iterator<ChanKanWeiXiuBean> iterator = datas.iterator();
                    while (iterator.hasNext()) {
                        ChanKanWeiXiuBean bean = iterator.next();
                        if (id.equals(bean.getId())) {
                            iterator.remove();
                            notifyDataSetChanged();
                            break;
                        }
                    }
                }
                if (entity.getEventType() == RepairPersenter.yiDao_fail) {
                    ByAlert.alert(entity.getData());
                }
                //修理完成
                if (entity.getEventType() == RepairPersenter.theEnd_success) {
//                    ByAlert.alert(entity.getData());
                    Iterator<ChanKanWeiXiuBean> iterator = datas.iterator();
                    while (iterator.hasNext()) {
                        ChanKanWeiXiuBean bean = iterator.next();
                        if (id.equals(bean.getId())) {
                            iterator.remove();
                            notifyDataSetChanged();
                            break;
                        }
                    }
                    if (SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("0")
                            ||SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("2")){//租客
                        Intent intent = new Intent(context,
                                ActivityXiuLiZhiFu.class);
                        intent.putExtra("id", id);
                        intent.putExtra("money", money);
                        intent.putExtra("type", repairStatus);
                        context.startActivity(intent);
                    }else if (SPUtils.get(context,null,SPContants.BrokerValidate,"").equals("1")){//经纪人
                        ByAlert.alert("经纪人需到联盟店后台支付");
                    }
                }
                if (entity.getEventType() == RepairPersenter.theEnd_fail) {
                    ByAlert.alert(entity.getData());
                }	else if (entity.getEventType() == RepairPersenter.xiugaiMoney_success) {
                    ByAlert.alert(entity.getData());

                    money = reMoney;
                    repairPersenter.xiuLiEnd(id, uid);


                } else if (entity.getEventType() == RepairPersenter.xiugaiMoney_fail) {
                    ByAlert.alert(entity.getData());
                }
            }
        }
    }

    class EditextDialog extends RxDialog{
        TextView tv_sure,tv_cancel;
        EditText modifyPrice;
        ChanKanWeiXiuBean t;
        Context context;
        public EditextDialog(Context context, int themeResId) {
            super(context, themeResId);
            initViewData();
        }

        public EditextDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
            super(context, cancelable, cancelListener);
            initViewData();
        }

        public EditextDialog(Context context) {
            super(context);
            initViewData();
        }

        public EditextDialog(Context context, float alpha, int gravity) {
            super(context, alpha, gravity);
            initViewData();
        }

        public EditextDialog(Context context,ChanKanWeiXiuBean t){
            super(context);
            this.context=context;
            this.t=t;
            initViewData();
        }
        private void initViewData() {
            View dialogView = LayoutInflater.from(context).inflate(R.layout.edittextdialog, null);
            modifyPrice = (EditText) dialogView.findViewById(R.id.modifyPrice);
            tv_sure = (TextView) dialogView.findViewById(R.id.tv_sure);
            tv_cancel = (TextView) dialogView.findViewById(R.id.tv_cancel);
            setContentView(dialogView);
//            dialogView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    Log.d("=====","====");
//                }
//            });
            tv_cancel.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            tv_sure.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!modifyPrice.getText().toString().equals("")){
                        id = t.getId();
                        repairStatus = t.getRepairstatus();
                        money=t.getMoney();
                        reMoney=modifyPrice.getText().toString();
                        repairPersenter.xiuGaiMoney(t.getId(), uid,modifyPrice.getText().toString(), "");
                        dismiss();
                    }else {
                        ByAlert.alert("请填写金额");
                    }

                }
            });
        }


    }
}
